﻿// XmlVisualizer
// Copyright (C) 2009  Roman Betík - rbetik@gmail.com

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using XmlVisualizer.Models;
using XmlVisualizer.Exceptions;

namespace XmlVisualizer.Controls {
	/// <summary>
	/// Interaction logic for ucTextNode.xaml
	/// UserControl representing TextNode in xml document.
	/// </summary>
	public partial class ucTextNode : UserControl {
		#region Properties
		/// <summary>
		/// Gets or sets the displayed text value of this user control.
		/// </summary>
		public String Text {
			get { return (String)GetValue(TextProperty); }
			set { SetValue(TextProperty, value); }
		}
		/// <summary>
		/// Identifies the XmlVisualizer.Controls.ucTextNode.TextProperty.
		/// </summary>
		public static DependencyProperty TextProperty;

		private XNode node;
		/// <summary>
		/// Gets or sets the XNode for this user control.
		/// </summary>
		public XNode Node {
			get { return node; }
			set { node = value; }
		}
		#endregion

		#region Constructors
		/// <summary>
		/// Initializes static fields.
		/// </summary>
		static ucTextNode() {
			FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTextChanged);
			TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(ucTextNode), metadata);
		}

		/// <summary>
		/// Initializes a new instance of the ucTextNode class.
		/// </summary>
		public ucTextNode() {
			InitializeComponent();
		}

		/// <summary>
		/// Initializes a new instance of the ucTextNode class, adding a specified XNode as the underlying node.
		/// </summary>
		/// <param name="node">XText node to be added as the initial node.</param>
		/// <exception cref="System.ArgumentNullException">node is null.</exception>
		public ucTextNode(XNode node)
			: this() {
			if (node != null) {
				Text = node.ToString();
				this.node = node;
			} else {
				throw new ArgumentNullException("node");
			}
		}
		#endregion

		#region Event handlers
		/// <summary>
		/// Callback for PropertyChangedCallback in FrameworkPropertyMetadata class.
		/// </summary>
		/// <param name="d">The DependencyObject on which the property has changed value.</param>
		/// <param name="e">Event data that is issued by any event that tracks changes to the effective value of this property.</param>
		private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
			if (d is ucTextNode) {
				ucTextNode node = d as ucTextNode;
				if (e.NewValue is String) {
					if (node.Node is XText) {
						String newValue = e.NewValue as String;
						XText textNode = node.Node as XText;
						textNode.Value = newValue;
					}
				}
			}
		}
		#endregion
	}
}